package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.CategoryDto;
import io.geekidea.boot.shop.entity.Category;
import io.geekidea.boot.shop.mapper.CategoryMapper;
import io.geekidea.boot.shop.query.app.AppCategoryQuery;
import io.geekidea.boot.shop.query.web.CategoryQuery;
import io.geekidea.boot.shop.service.base.api.CategoryService;
import io.geekidea.boot.shop.vo.app.AppCategoryVo;
import io.geekidea.boot.shop.vo.web.CategoryVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addCategory(CategoryDto dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        return save(category);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateCategory(CategoryDto dto) {
        Integer id = dto.getId();
        Category category = getById(id);
        if (category == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, category);
        return updateById(category);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteCategory(Integer id) {
        return removeById(id);
    }

    @Override
    public CategoryVo getCategoryById(Integer id) {
        return categoryMapper.getCategoryById(id);
    }

    @Override
    public Paging<CategoryVo> getCategoryPage(CategoryQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<CategoryVo> list = categoryMapper.getCategoryPage(query);
        Paging<CategoryVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppCategoryVo getAppCategoryById(Integer id) {
        return categoryMapper.getAppCategoryById(id);
    }

    @Override
    public Paging<AppCategoryVo> getAppCategoryPage(AppCategoryQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppCategoryVo> list = categoryMapper.getAppCategoryPage(query);
        Paging<AppCategoryVo> paging = new Paging<>(list);
        return paging;
    }

}
